package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.AppVersionManageService;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.service.dto.AppVersionManageDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.hibernate.exception.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing AppVersionManage.
 */
@RestController
@RequestMapping("/api")
public class AppVersionManageResource {

    private final Logger log = LoggerFactory.getLogger(AppVersionManageResource.class);

    private static final String ENTITY_NAME = "appVersionManage";

    private final AppVersionManageService appVersionManageService;

    public AppVersionManageResource(AppVersionManageService appVersionManageService) {
        this.appVersionManageService = appVersionManageService;
    }

    /**
     * POST  /app-version-manages : Create a new appVersionManage.
     *
     * @param appVersionManageDTO the appVersionManageDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new appVersionManageDTO, or with status 400 (Bad Request) if the appVersionManage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/app-version-manages")
    @Timed
    public ResponseEntity<AppVersionManageDTO> createAppVersionManage(@RequestBody AppVersionManageDTO appVersionManageDTO) throws URISyntaxException {
        log.debug("REST request to save AppVersionManage : {}", appVersionManageDTO);
        try {
            if (appVersionManageDTO.getId() != null) {
                return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new appVersionManage cannot already have an ID")).body(null);
            }
            AppVersionManageDTO result = appVersionManageService.save(appVersionManageDTO);
            return ResponseEntity.created(new URI("/api/app-version-manages/" + result.getId()))
                .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
                .body(result);
        } catch (Exception ex) {
            if (ex instanceof ConstraintViolationException || ex instanceof DataIntegrityViolationException) {
                return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "The value is not unique")).body(null);
            } else {
                return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", ex.getMessage())).body(null);
            }
        }

    }

    /**
     * PUT  /app-version-manages : Updates an existing appVersionManage.
     *
     * @param appVersionManageDTO the appVersionManageDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated appVersionManageDTO,
     * or with status 400 (Bad Request) if the appVersionManageDTO is not valid,
     * or with status 500 (Internal Server Error) if the appVersionManageDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/app-version-manages")
    @Timed
    public ResponseEntity<AppVersionManageDTO> updateAppVersionManage(@RequestBody AppVersionManageDTO appVersionManageDTO) throws URISyntaxException {
        log.debug("REST request to update AppVersionManage : {}", appVersionManageDTO);
        if (appVersionManageDTO.getId() == null) {
            return createAppVersionManage(appVersionManageDTO);
        }
        AppVersionManageDTO result = appVersionManageService.save(appVersionManageDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, appVersionManageDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /app-version-manages : get all the appVersionManages.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of appVersionManages in body
     */
    @GetMapping("/app-version-manages")
    @Timed
    public List<AppVersionManageDTO> getAllAppVersionManages() {
        log.debug("REST request to get all AppVersionManages");
        return appVersionManageService.findAll();
    }

    /**
     * GET  /app-version-manages/:id : get the "id" appVersionManage.
     *
     * @param id the id of the appVersionManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the appVersionManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/app-version-manages/{id}")
    @Timed
    public ResponseEntity<AppVersionManageDTO> getAppVersionManage(@PathVariable Long id) {
        log.debug("REST request to get AppVersionManage : {}", id);
        AppVersionManageDTO appVersionManageDTO = appVersionManageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(appVersionManageDTO));
    }

    /**
     * DELETE  /app-version-manages/:id : delete the "id" appVersionManage.
     *
     * @param id the id of the appVersionManageDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/app-version-manages/{id}")
    @Timed
    public ResponseEntity<Void> deleteAppVersionManage(@PathVariable Long id) {
        log.debug("REST request to delete AppVersionManage : {}", id);
        appVersionManageService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * 获取最新版本信息
     *
     * @return the ResponseEntity with status 200 (OK) and with body the appVersionManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/app-version-manages/latest")
    @Timed
    public ResponseEntity<AppVersionManageDTO> getLatestVersion() {
        AppVersionManageDTO appVersionManageDTO = appVersionManageService.getLatestVersion();
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(appVersionManageDTO));
    }
}
